package queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class MyBlockingQueue1 {
    public static void main(String[] args) throws InterruptedException {
        //容纳3个元素的 有界阻塞队列
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        queue.add("a");//添加元素
        queue.add("b");
        queue.add("c");
        //queue.add("d"); 越界抛异常：java.lang.IllegalStateException: Queue full
        System.out.println(queue.element());//返回队列头是哪个元素 a
        queue.remove();//移除元素
        queue.remove();
        queue.remove();
        //queue.remove();j越界抛异常：ava.util.NoSuchElementException

        queue.offer("aa");//向队列添加元素
        queue.offer("bb");
        queue.offer("cc");
        System.out.println(queue.offer("dd")); //越界返回false
        System.out.println(queue.peek());//返回队列头元素
        queue.poll();
        queue.poll();
        queue.poll();
        System.out.println(queue.poll());//越界返回null

        queue.put("aa");
        queue.put("bb");
        queue.put("cc");
        //queue.put("dd"); //越界 阻塞，一致等着，知道能插进去为止
        queue.take();
        queue.take();
        queue.take();
        //queue.take();//越界 阻塞，一致等着，知道能插进去为止

        queue.offer("aa",2L, TimeUnit.SECONDS);
        queue.offer("aa",2L, TimeUnit.SECONDS);
        queue.offer("aa",2L, TimeUnit.SECONDS);
        queue.offer("aa",2L, TimeUnit.SECONDS);//阻塞等待2s，依然插不进去，返回false
    }
}
